#pragma once

#include <iostream>
#include <unistd.h>
#include<memory>
#include"socket.hpp"
#include "log.hpp"
#include <sys/poll.h>

using namespace logmodule;
using namespace socketmodule;
const static int size = sizeof(fd_set)*8;
const static int  defaultfd = -1;
class PollServer
{
    
    public:
   PollServer(int port):_listensock(std::make_unique<Tcpsocket>())
    {
        _listensock->BuildTcpsocket(port);
        for(int i= 0;i<size;i++)
        {
            _fds[i].fd = defaultfd;
            _fds[i].events = 0;
            _fds[i].revents = 0;

        }
       _fds[0].fd = _listensock->Fd();
       _fds[0].events = POLLIN;
    }
    void accepter()
    {
         inetaddr client;
        int fd = _listensock->accepting(&client);
        int pos = 0;
        if(fd>=0)
        {
            //将新的fd托管到select,放入辅助数组
            for(;pos< size ;pos++)
            {
                if(_fds[pos].fd==defaultfd)
                {
                    _fds[pos].fd = fd;
                }
            }
            if(pos == size)
            {
                close(fd);
            }
            else
            {
                _fds[pos].fd = fd;
                _fds[pos].events = POLLIN;
                _fds[pos].revents = 0;
            }
        }
    }
    void handerevent()
    {
        for(int i= 0;i<size;i++)
        {
            if(_fds[i].fd==defaultfd)
            {
                continue;
            }
            //合法的文件描述符
            if(_fds[i].events&POLLIN)
            {
                //有这个文件描述符，读就绪了
                if(_fds[i].fd == _listensock->Fd())
                {
                    //新连接来了
                    accepter();
                }
                else
                {
                    Receiver(i);//其他事件就绪
                }
            }
        }
    
        // inetaddr client;
        // int fd = _listensock->accepting(&client);
        // int pos = 0;
        // if(fd>=0)
        // {
        //     //将新的fd托管到select,放入辅助数组
        //     for(;pos< size ;pos++)
        //     {
        //         if(fd_array[pos]==defaultfd)
        //         {
        //             fd_array[pos] = fd;
        //         }
        //     }
        //     if(pos == size)
        //     {
        //         close(fd);
        //     }
        // }
    }
    void Receiver(int pos)
    {
        //读东西不会阻塞
        char buffer[1024];
        ssize_t n = recv(_fds[pos].fd,buffer,sizeof(buffer)-1,0);
        if(n>0)
        {
            buffer[n] = 0;
            std::cout<<""<<std::endl;
        }
        else if(n ==0)
        {
            //客户端退出
            //在辅助数组中去掉
             _fds[pos].fd = defaultfd;
                _fds[pos].events = 0;
                _fds[pos].revents = 0;

            //关闭
            close(_fds[pos].fd);
        }
        else
        {

        }
    }
    void start()
    {
        _isrunning = true;
        while(_isrunning)
        {
            int timeout = 1000;
            int n = poll(_fds,size,timeout);
            switch(n)
            {
                case -1 :
                break;
                case 0 :
                break;
                default:
                //有事件就绪
                //要处理事件了
                handerevent();
                break;
            }
        }
        _isrunning = false;
    }
    ~PollServer()
    {}
    private:
    std::unique_ptr<Socket> _listensock;
    bool _isrunning;
   struct pollfd _fds[size];
};
